package com.jiawa.train.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.utils.SnowUtil;
import com.jiawa.train.member.domain.Member;
import com.jiawa.train.member.domain.MemberExample;
import com.jiawa.train.member.mapper.MemberMapper;
import com.jiawa.train.member.req.MemberLoginReq;
import com.jiawa.train.member.req.MemberRegisterReq;
import com.jiawa.train.member.req.MemberSendCodeReq;
import com.jiawa.train.member.resp.MemberLoginResp;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName MemberService
 * @Description
 * @Author GateOfRust
 * @Date 2024/5/15 下午9:26
 * @Version 1.0
 */

@Service
public class MemberService {
    private static final Logger LOG = LoggerFactory.getLogger(MemberService.class);

    @Resource
    private MemberMapper memberMapper;

    public Integer count(){
        return (int) memberMapper.countByExample(null);
    }

    //使用实体类来解接收请求参数
    public Long register(MemberRegisterReq req) {
        String mobile = req.getMobile();
        //建立查询条件 =
        Member list = selectByMobile(mobile); //为空返回null

        if (ObjectUtil.isNotNull(list)){ //不为空抛出错误：手机号已存在
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
//            return list.get(0).getId();
        }

        Member member = new Member();
        //测试主键冲突异常
//        member.setId(1L);
//        member.setId(System.currentTimeMillis());
        //雪花算法 两个参数：数据中心id和机器id
//        member.setId(IdUtil.getSnowflake(1, 1).nextId());
        //使用封装好的数据
        member.setId(SnowUtil.getSnowflakeNextId());
        member.setMobile(mobile);
        memberMapper.insert(member);
        return member.getId();
    }

    public void sendCode(MemberSendCodeReq req) {
        //通过请求参数获取手机号信息
        String mobile = req.getMobile();
        Member list = selectByMobile(mobile);

        //如果手机号不存在则插入一条数据
        if (ObjectUtil.isNull(list)){
            LOG.info("手机号不存在，插入一条记录");
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
        }else {
            LOG.info("手机号存在，不插入一条记录");
        }

        //生成验证码
//        String code = RandomUtil.randomString(4);
        String code = "8888"; //测试用
        LOG.info("生成短信验证码：{}",code);

        //保存短信记录表：手机号，短信验证码，有效期，是否已使用，业务类型，发送时间，使用时间


        //对接短信通道，发送短信
    }

    public MemberLoginResp Login(MemberLoginReq req) {
        //通过请求参数获取手机号信息
        String mobile = req.getMobile();
        String code = req.getCode();
        Member list = selectByMobile(mobile);

        //如果手机号不存在则抛出错误
        if (ObjectUtil.isNull(list)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }

        //校验短信验证码
        if (!"8888".equals(code)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_ERROR_EXIST);
        }

        //使用工具类封装返回的数据为MemberLoginResp类型
        return BeanUtil.copyProperties(list, MemberLoginResp.class);
    }



    //查询手机号是否存在
    private Member selectByMobile(String mobile) {
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> list = memberMapper.selectByExample(memberExample);
        //判断手机号在数据库是否存在
        //不存在返回 null，存在则返回Member实体类
        if (CollUtil.isEmpty(list)){
            return null;
        }else {
            return list.get(0);
        }
    }

}
